package cn.edu.xust.api.controller;

import cn.edu.xust.configuration.exception.Default200Exception;
import cn.edu.xust.configuration.exception.ErrorConstant;
import cn.edu.xust.entity.SysRoleEntity;
import cn.edu.xust.entity.SysUserroleEntity;
import cn.edu.xust.service.SysRoleService;
import cn.edu.xust.service.SysRoleprivilegeService;
import cn.edu.xust.service.SysUserroleService;
import cn.edu.xust.util.R;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;


/**
 * @author ufo
 * @email 2575829997@qq.com
 * @date 2022-09-06 17:32:09
 */
@RestController
@RequestMapping("/api/sysrole")
public class ApiSysRoleController {
    @Autowired
    private SysRoleService sysRoleService;
    @Autowired
    private SysUserroleService sysUserroleService;
    @Autowired
    private SysRoleprivilegeService sysRoleprivilegeService;

    /**
     * 创建一个角色
     *
     * @param sysRoleEntity 系统角色实体
     */
    @PostMapping("/create")
    @RequiresPermissions(value={"11"},logical = Logical.OR)
    public R create(@RequestBody SysRoleEntity sysRoleEntity) throws Default200Exception {


        //检查编号是否重复
        SysRoleEntity byId = sysRoleService.getById(sysRoleEntity.getRoleid());
        if (byId != null) {
            throw new Default200Exception(ErrorConstant.role_exist);
        }
        //检查名称是否重复
        SysRoleEntity byId2 = sysRoleService.getByCname(sysRoleEntity.getCname());
        if (byId2 != null) {
            throw new Default200Exception(ErrorConstant.role_name_exist);
        }
        sysRoleEntity.setCt(new Date());
        sysRoleEntity.setUt(new Date());
        boolean save = sysRoleService.save(sysRoleEntity);
        if (!save) {
            R.error();
        }
        return R.ok();
    }


    /**
     * 删除一个角色
     */
    @PostMapping("/del")
    @RequiresPermissions(value={"12"},logical = Logical.OR)
    public R del(@RequestBody SysRoleEntity sysRoleEntity) throws Default200Exception {


        //检查名称 编号 是否存在
        Integer roleid = sysRoleEntity.getRoleid();
        SysRoleEntity byId = sysRoleService.getByNameAndId(sysRoleEntity.getCname(), roleid);
        if (byId == null) {
            throw new Default200Exception(ErrorConstant.role_not_exist);
        }

        //检查当前是否有用户使用这个角色
        List<SysUserroleEntity> sysUserroleEntities = sysUserroleService.getByroleid(roleid);
        if (!sysUserroleEntities.isEmpty()) {
            throw new Default200Exception(ErrorConstant.role_have_been_use);
        }

        //删除角色
        boolean b = sysRoleService.removeById(roleid);
        //删除权限
        sysRoleprivilegeService.deleteByRoleid(roleid);

        if (!b) {
            R.error();
        }
        return R.ok();
    }

    /**
     * 修改一个角色 仅限角色名称
     */
    @RequiresPermissions(value={"13"},logical = Logical.OR)
    @PostMapping("/updatename")
    public R updatename(@RequestBody SysRoleEntity sysRoleEntity) throws Default200Exception {
        //检查名称 编号 是否存在
        SysRoleEntity byId = sysRoleService.getById(sysRoleEntity.getRoleid());
        if (byId == null) {
            throw new Default200Exception(ErrorConstant.role_not_exist);
        }
        //检查名称是否重复
        SysRoleEntity byId2 = sysRoleService.getByCname(sysRoleEntity.getCname());
        if (byId2 != null) {
            throw new Default200Exception(ErrorConstant.role_name_exist);
        }
        boolean b = sysRoleService.updatename(sysRoleEntity);
        if (!b) {
            R.error();
        }
        return R.ok();
    }



}
